var updateCodelibDialog = new Dialog( {
	showdiv : "updateCodelibDialog",
	title : "修改代码库信息",
	x : 560,
	y : 360
});
rootDialog.childDialog = updateCodelibDialog;
var createCodelibDialog = new Dialog( {
	showdiv : "createCodelibDialog",
	title : "创建代码库信息",
	x : 560,
	y : 360
});
rootDialog.childDialog = createCodelibDialog;
var codeType = "";
var codeFilterValue = "";
function initCodeTypeSelect() {
	Codelib.getCodeTypeList( function(data) {
		DWRUtil.addOptions("codeCombox", data, "id", "name");
		codeType = DWRUtil.getValue("codeCombox");
		if (codeType) {
			send(code);
		}
	});
}
function initCodelib(codeid) {
	if (codeType != "" && codeid != null && codeid != "") {
		Codelib.chakan(codeType, codeid, function(data) {
			for ( var i = 0; i < data.length; i++) {
				if (data[i].tname == 'id') {
					$('id').setAttribute("ReadOnly", true);
				}
				if (data[i].tname == 'sign') {
					DWRUtil.setValue(data[i].tname, data[i].tval);
				} else {
					$(data[i].tname).value = data[i].tvalue;
				}
			}
		});
	}
}
function newCodelib() {
	var id = trim_value(DWRUtil.getValue("id"));
	var sign = DWRUtil.getValue("sign");
	var type = DWRUtil.getValue("type");
	var name = trim_value(DWRUtil.getValue("name"));
	var checkpname = true;
	Codelib.checkPName(codeType, id, name, type, function(data) {
		if (data == 1) {
			checkpname = true;
			$Q("codeerror").innerHTML = "正确！";
		} else {
			checkpname = false;
			$Q("codeerror").innerHTML = "代码名称已存在！";
		}
		if (checkpname) {
			if (type == "update") {
				Codelib.update(codeType, id, name, sign, function(data) {
					if (data == 1) {
						send(code);
						alert("修改代码库信息成功！");
					} else {
						alert("修改代码库信息失败！");
					}
					closeDialog(updateCodelibDialog);
				});
			} else {
				Codelib.save(codeType, id, name, sign, function(data) {
					if (data == 1) {
						send(code);
						alert("新建代码库信息成功！");
					} else {
						alert("新建代码库信息成功！");
					}
					closeDialog(createCodelibDialog);
				});
			}
		}
	});
}
var chakancellfuncs = [ function(data) {
	return data.title;
}, function(data) {
	return data.tvalue;
} ];
var cellfuncs = [
		function(data) {
			return data.id;
		},
		function(data) {
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				rootDialog.showdivWidth = 200;
				rootDialog.showdivHeight = 100;
				rootDialog.title = "查看代码详细";
				openDialog(rootDialog, "", function() {
					var chakantable = document.createElement("table");
					chakantable.style.align = "center";
					chakantable.className = "table";
					rootDialog.contentdiv.appendChild(chakantable);
					var chakantbody = document.createElement("tbody");
					chakantbody.setAttribute("id", "chakantbody");
					chakantable.appendChild(chakantbody);
					Codelib.chakan(codeType, data.id, function(data) {
						DWRUtil.removeAllRows("chakantbody");
						DWRUtil.addRows("chakantbody", data, chakancellfuncs,
								tableclass);
					});
				});
			};
			href.innerHTML = data.name;
			return href;
		}, function(data) {
			return data.sign == "1" ? "有效" : "无效";
		}, function(data) {
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				openDialog(updateCodelibDialog, "add.htm", function() {
					DWRUtil.setValue("type", "update");
					initCodelib(data.id);
				});
			};
			href.innerHTML = "\u4fee\u6539";
			return href;
		}, function(data) {
			if (data.id) {
				var href = document.createElement("a");
				href.setAttribute("href", "#");
				href.onclick = function() {
					if (confirm("确认删除代码(" + data.name + ")吗?")) {
						Codelib.deleteCode(codeType, data.id, function(data) {
							if (data) {
								send(code);
								alert("删除成功！");
							} else {
								alert("删除失败！");
							}
						});
					}
				};
				href.innerHTML = "\u5220\u9664";
				return href;
			} else {
				return "";
			}
		} ];

var code = {
	navigationsize : 10,
	paramname : "code",
	page : 1,
	funcname : "Codelib.findAllPage(codeType,)",
	cellfuncs : cellfuncs,
	pagesize : 10,
	count : "Codelib.getCount(codeType,)",
	idname : "codeliblistZone"
};
function codeFilter(value, type) {
	if (type == 'id') {
		code.funcname = "Codelib.findByLikeIdPage(codeType,'" + value + "',)";
		code.count = "Codelib.getCountById(codeType,'" + value + "',)";
	} else if (type == 'name') {
		code.funcname = "Codelib.findByLikeNamePage(codeType,'" + value + "',)";
		code.count = "Codelib.getCountByName(codeType,'" + value + "',)";
	}
	send(code);
}
var pageuuid = code.paramname;
window.onload = function() {
	initDWR();
	// useLoading("../expimages/blue-loading.gif", "img");
	initCodeTypeSelect();
};